#ifndef AE_DEVICE_H
#define AE_DEVICE_H

#include <QObject>
#include "tag_uhf.h"
#include "unistd.h"
#include "ultils.h"
#include <vector>
#include "rfidlib_reader.h"
#include "record_alarm.h"
#include "record_day.h"
#include "record_realtime.h"
#include "scan_record.h"
using namespace std;

//设备类型
#define RDTYPE_HF 0
#define RDTYPE_UHF 1

//空中协议类型
#define AIR_ISO14443A			1   //空中协议参数类型
#define AIR_ISO15693			(1<<1)
#define AIR_ISO18000p3m3		(1<<2)
#define AIR_ISO180006C			(1<<3)
#define AIR_ISO14443B           (1<<4)
#define AIR_ST_ISO14443B        (1<<5)
#define AIR_SONY_FELICA         (1<<6)
#define AIR_NFC_FORUM_TYPE1     (1<<7)


class CAEDevice : public QObject
{
    Q_OBJECT
public:
    int RDType_ID=-1;//0:HF 1:UHF
    bool m_opened=false;
    bool is_inventory=false;

    //lsg control center
    bool is_get_records=false;

    //scan/buffer mode
    bool is_scan_mode=false;

    explicit CAEDevice(QObject *parent = nullptr);
    virtual ~CAEDevice();

    DWORD get_antenna_count();
    int GetReaderInfor(char *pinfo, int &len);

    int Close();
    int Open(LPTSTR connStr);

    int start_inventory(const char *m_RDType, BYTE antennas[],BYTE ant_count);
    void stop_inventory();

    virtual int func_Inventory();
    virtual int func_AccessTag_GetInfor(CTag &tag, BYTE *dsfid, BYTE *afi,DWORD *block_size,DWORD *block_num, BYTE *ic_ref);
    virtual int func_AccessTag_Check_EAS(CTag &tag, BYTE &eas_sta);
    virtual int func_AccessTag_EnDisable_EAS(CTag &tag, bool bEnable);
    virtual int func_AccessTag_Write_AFI(CTag &tag, BYTE afi);

    //HF
    virtual int func_AccessTag_WriteData(CTag &tag, DWORD block_addr, DWORD block_num, BYTE *data, DWORD dlen);
    virtual int func_AccessTag_ReadData(CTag &tag, DWORD block_addr, DWORD block_num, BYTE *data, DWORD *dlen);

    //UHF
    virtual int func_AccessTag_WriteData(CTag &tag, DWORD accessPwd,BYTE mem_type, DWORD mem_addr_word, DWORD mem_num_word, BYTE *data, DWORD dlen);
    virtual int func_AccessTag_ReadData(CTag &tag, DWORD accessPwd,BYTE mem_type, DWORD mem_addr_word, DWORD mem_num_word, BYTE *data, DWORD &dlen);
    virtual int func_AccessTag_UHF_Kill_Tag(CTag &tag, DWORD accessPwd, DWORD killPwd, BYTE rfu);

    //LSG Control Center
    int start_get_records(DWORD Addtionflag, BYTE gate_id);
    void stop_get_records();
    virtual int func_LSGCC_SinglelDoor_Alarm(BYTE gate_id);
    virtual int func_LSGCC_SinglelDoor_SetOutput(BYTE gate_id,BYTE output_ids[],BYTE output_len,BYTE type,BYTE freq,DWORD at,DWORD dt);
    virtual int func_LSGCC_SinglelDoor_SetAtmosphereLight(BYTE gate_id,BYTE light_id,BYTE type,BYTE freq,DWORD at,DWORD dt);
    virtual int func_LSGCC_SinglelDoor_GetIRTriggerCount(BYTE gate_id,DWORD ir_ids[],BYTE* ir_nums);
    virtual int func_LSGCC_InitPC(BYTE type,DWORD inPC,DWORD outPC);
    virtual int func_LSGCC_GetPC(BYTE option, DWORD * pcInTotal, DWORD * pcOutTotal, DWORD * pcInDay, DWORD * pcOutDay);
    virtual int func_LSGCC_GetSystemTime(DWORD* year,BYTE* month,BYTE* day,BYTE* hour,BYTE* minute,BYTE* second);
    virtual int func_LSGCC_SetSystemTime(DWORD year,BYTE month,BYTE day,BYTE hour,BYTE minute,BYTE second);
    virtual int func_LSGCC_GetRecords(bool* deleteFlag,DWORD Addtionflag,BYTE gate_id,DWORD* total_in,DWORD* total_out,DWORD* day_in,DWORD* day_out);

    //RPAN/UHF-RPAN/READER
    virtual int func_Scan_Mode_Fetch_Tags(DWORD scan_mode_flag);
    int start_scan_mode(const char *m_RDType);
    void stop_scan_mode();
    void reset_scan_mode_flag()
    {
        this->scan_mode_flag = 0x00;
    }

signals:
    //register unstandard data type

    //hf/uhf reader
    void sgnl_inventory_data_hf(int tag_count,vector<CTag_HF> tags,int use_time,int loop_count);
    void sgnl_inventory_data_uhf(int tag_count,vector<CTag_UHF> tags,int use_time,int loop_count);
    void sgnl_inventory_end_loop(int iret);//stop inventory ,thread ended

    //lsg control center
    void sgnl_lsgcc_get_records(vector<CRecord_Alarm> rcd_alarm,vector<CRecord_Day> rcd_day,vector<CRecord_RealTime> rcd_realtime,
                                int total_in,int total_out,int day_in,int day_out,int use_time,int loop_count);
    void sgnl_lsgcc_get_records_end_loop(int iret);//stop get records ,thread ended

    //scan mode
    void sgnl_scan_mode_data(int tag_count,vector<CScan_Record> tags,int use_time,int loop_count);
    void sgnl_scan_mode_end_loop(int iret);//stop scan mode ,thread ended

public slots:

protected:
    static void *Inventory_Loop(void *hr);
    RFID_READER_HANDLE hRdr=NULL;
    vector<CTag_HF>  m_tags_hf;
    vector<CTag_UHF>  m_tags_uhf;
    bool loop=false;
    BYTE air_type=AIR_ISO15693;
    BYTE antennas[64]={0};
    BYTE ant_count=64;

    static void * GetRecords_Loop(void *hr);
    vector<CRecord_Alarm>  m_rcd_alarm;
    vector<CRecord_Day>  m_rcd_day;
    vector<CRecord_RealTime>  m_rcd_realtime;
    DWORD Get_Record_Addtionflag = 0;
    BYTE Get_Record_Gate_id = 0;

    static void *Scan_Mode_Loop(void *hr);
    DWORD scan_mode_flag = 0;
    vector<CScan_Record>  m_scan_records;

private:
    BYTE get_air_type(const char *m_RDType);
};

#endif // AE_DEVICE_H
